package com.saxon;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.file.Paths;

import org.ofdrw.converter.ConvertHelper;
import org.ofdrw.converter.FontLoader;

import com.aspose.words.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;
import com.saxon.pdfToOfdBao.OFDRender;


/***
 * 相关格式转pdf
 * @author saxon
 * import com.aspose.words.Document;这个包找不到
 * jar包如下：
 * 项目的resources目录中有，以及百度云
 * 链接: https://pan.baidu.com/s/1riuxk-1koRSNZkK-D1G9eA  密码: iqvo
 * 自行导入到自己的私服上或者：
 * 右键项目 -> Build Path -> Configure Build Path ->Libraries ->Add External JARS 
 * 不过打包的时候需自行解决，
 * https://www.cnblogs.com/lukelook/p/8673778.html
 * 
 * @date 2021年1月29日
 */
public class PdfUtils {
	
	/***
	 * word转pdf
	 * @param wordFile  word文件路径
	 * @param pdfFile   pdf文件路径
	 */
	public static boolean wordToPdf(String wordFile,String pdfFile){
		boolean bool=true;
		try {
			
            //doc路径
           Document document = new Document(wordFile);
           //pdf路径
           File outputFile = new File(pdfFile);
           //操作文档保存
           document.save(outputFile.getAbsolutePath(), com.aspose.words.SaveFormat.PDF);
       } catch (Exception e) {
           e.printStackTrace();
           bool=false;
       }
		return bool;
	}
	
	/***
	 * 图片转pdf
	 * @param imgFile
	 * @param pdfFile
	 */
	public static boolean imageToPdf(String imgFile, String pdfFile){
		boolean bool=true;
		try {
	    	com.itextpdf.text.Document document = new com.itextpdf.text.Document();
	        OutputStream os = new FileOutputStream(new File(pdfFile));
	        PdfWriter.getInstance(document,os);
	        document.open();
	        Image image = Image.getInstance(imgFile);
	        float documentWidth = document.getPageSize().getWidth() - document.leftMargin() - document.rightMargin();
	        float documentHeight = documentWidth / 580 * 450;//重新设置宽高
	        image.scaleAbsolute(documentWidth, documentHeight);//重新设置宽高
	        document.add(image);
	        document.close();
		} catch (Exception ex) {
			ex.printStackTrace();
			bool=false;
	    }
		return bool;
    }
	
	
	
	/***
	 * pdf转ofd
	 * @param pdfFile  pdf文件路径
	 * @param ofdFile  ofd文件路径
	 */
	public static boolean pdfToOfd(String pdfFile,String ofdFile){
		boolean bool=true;
		try {
			//转换
			 OFDRender.convertPdfToOfd(new FileInputStream(pdfFile), new FileOutputStream(ofdFile));
       } catch (Exception e) {
           e.printStackTrace();
           bool=false;
       }
		return bool;
	}
	
	
	/***
	 * ofd转pdf
	 * @param ofdFile  ofd文件路径
	 * @param pdfFile  pdf文件路径
	 */
	public static boolean ofdTopdf(String ofdFile,String pdfFile){
		boolean bool=true;
		try {
			FontLoader.getInstance()
            .addAliasMapping(null, "小标宋体", "方正小标宋简体", "方正小标宋简体")
            .addAliasMapping(null, "KaiTi_GB2312", "楷体", "楷体")
            .addSimilarFontReplaceRegexMapping(null, ".*Kai.*", null, "楷体")
            .addSimilarFontReplaceRegexMapping(null, ".*Kai.*", null, "楷体")
            .addSimilarFontReplaceRegexMapping(null, ".*MinionPro.*", null, "SimSun")
            .addSimilarFontReplaceRegexMapping(null, ".*SimSun.*", null, "SimSun")
            .addSimilarFontReplaceRegexMapping(null, ".*Song.*", null, "宋体")
            .addSimilarFontReplaceRegexMapping(null, ".*MinionPro.*", null, "SimSun");
	        FontLoader.setSimilarFontReplace(true);
			//转换
	        ConvertHelper.toPdf(Paths.get(ofdFile), Paths.get(pdfFile));
       } catch (Exception e) {
           e.printStackTrace();
           bool=false;
       }
		return bool;
	}
	
	
	
	
	
	
	
	public static void main(String[] args) {
//      word转pdf
//		String wordFile = "D:\\一些资料\\签章加解密接口.docx";
//		String pdfFile1 = "D:\\\\一些资料\\\\签章加解密接口.pdf";
//		if(wordToPdf(wordFile,pdfFile1)){
//			System.out.println("word转pdf成功");
//		}else{
//			System.out.println("word转pdf失败");
//		}
//		String imgFile="C:\\Users\\Saxon\\Desktop\\logo.png";
//		String pdfFile2="C:\\Users\\Saxon\\Desktop\\logo.pdf";
//		if(imageToPdf(imgFile,pdfFile2)){
//			System.out.println("img转pdf成功");
//		}else{
//			System.out.println("img转pdf失败");
//		}
		//pdfToOfd("D:\\gitee_java_space\\PDF2OFD\\render\\src\\test\\resources\\n.pdf","d:\\n.ofd");
		
	}
}
